<template>
  <el-card 
    class="meal-card"
    v-loading="loading"
  >
    <template #header>
      <div class="card-header">
        <div class="meal-info">
          <el-tag :type="mealTypeTag" effect="light" round class="meal-type-tag">
            {{ mealTypeLabel }}
          </el-tag>
          <span class="time">{{ formatTime(record.recordTime) }}</span>
        </div>
        <div class="actions">
          <el-button 
            type="danger" 
            circle
            :icon="Delete"
            @click="$emit('delete', record)"
          />
        </div>
      </div>
    </template>

    <div v-if="!record.details?.length" class="empty-state">
      <el-empty description="暂无食物记录">
        <template #image>
          <el-icon :size="60"><Bowl /></el-icon>
        </template>
      </el-empty>
    </div>

    <div v-else class="food-list">
      <el-collapse accordion>
        <el-collapse-item 
          v-for="detail in record.details" 
          :key="detail.detailId"
        >
          <template #title>
            <div class="food-item-header">
              <el-avatar 
                :size="40"
                :src="detail.food?.imageUrl"
                :fallback="defaultFoodImage"
                fit="cover"
              />
              <div class="food-basic-info">
                <span class="food-name">{{ detail.food?.name }}</span>
                <span class="food-quantity">
                  {{ detail.quantity }}{{ detail.food?.unit }}
                </span>
              </div>
              <div class="food-calories">
                {{ Math.round(detail.food?.calories * detail.quantity) }}千卡
              </div>
            </div>
          </template>

          <div class="food-details">
            <div class="nutrition-grid">
              <div class="nutrition-item">
                <span class="label">蛋白质</span>
                <span class="value">{{ (detail.food?.protein * detail.quantity).toFixed(1) }}g</span>
              </div>
              <div class="nutrition-item">
                <span class="label">碳水</span>
                <span class="value">{{ (detail.food?.carbohydrates * detail.quantity).toFixed(1) }}g</span>
              </div>
              <div class="nutrition-item">
                <span class="label">脂肪</span>
                <span class="value">{{ (detail.food?.fat * detail.quantity).toFixed(1) }}g</span>
              </div>
            </div>
            
            <div class="food-actions">
              <el-button type="primary" link :icon="Edit" @click="$emit('edit', detail)">
                编辑
              </el-button>
            </div>
          </div>
        </el-collapse-item>
      </el-collapse>
    </div>

    <add-food-dialog
      v-model:visible="showAddFoodDialog"
      :record-id="record.recordId"
      @confirm="handleAddFoodConfirm"
    />
  </el-card>
</template>

<script setup>
import { computed, ref } from 'vue'
import { Edit, Delete, Bowl } from '@element-plus/icons-vue'
import AddFoodDialog from './AddFoodDialog.vue'

const defaultFoodImage = ref('https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png')

const props = defineProps({
  record: {
    type: Object,
    required: true,
    validator(value) {
      console.log('MealCard record prop:', value)
      return value && value.recordId && value.mealType
    }
  },
  loading: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['add-food', 'edit', 'delete'])

const mealTypeTag = computed(() => {
  const types = {
    '早餐': 'success',
    '午餐': 'warning',
    '晚餐': 'danger',
    '零食': 'info'
  }
  return types[props.record.mealType] || 'primary'
})

const mealTypeLabel = computed(() => {
  const labels = {
    '早餐': '早餐',
    '午餐': '午餐',
    '晚餐': '晚餐',
    '零食': '零食'
  }
  return labels[props.record.mealType] || props.record.mealType
})

const formatTime = (time) => {
  if (!time) return ''
  return time.substring(0, 5)
}

const showAddFoodDialog = ref(false)

const handleAddFoodConfirm = async (formData) => {
  emit('add-food', {
    mealType: formData.mealType,
    recordTime: formData.recordTime,
    foodId: formData.foodId,
    quantity: formData.quantity,
    food: formData.food
  })
}
</script>

<style scoped>
.meal-card {
  margin-bottom: 1rem;
  background-color: var(--el-bg-color);
  border: 1px solid var(--el-border-color-lighter);
  transition: all 0.3s ease;
  border-radius: 8px;
  overflow: hidden;
}

.meal-card:hover {
  border-color: var(--el-border-color);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: linear-gradient(to right, var(--el-color-primary-light-9), var(--el-bg-color));
  padding: 12px 20px;
  border-bottom: 1px solid var(--el-border-color-lighter);
}

.meal-info {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.meal-type-tag {
  font-weight: 600;
  font-size: 0.95rem;
  padding: 0.25rem 1rem;
}

.meal-type-tag:deep(.el-tag--success) {
  background-color: var(--el-color-success-light-8);
  border-color: var(--el-color-success-light-5);
  color: var(--el-color-success);
}

.meal-type-tag:deep(.el-tag--warning) {
  background-color: var(--el-color-warning-light-8);
  border-color: var(--el-color-warning-light-5);
  color: var(--el-color-warning);
}

.meal-type-tag:deep(.el-tag--danger) {
  background-color: var(--el-color-danger-light-8);
  border-color: var(--el-color-danger-light-5);
  color: var(--el-color-danger);
}

.meal-type-tag:deep(.el-tag--info) {
  background-color: var(--el-color-info-light-8);
  border-color: var(--el-color-info-light-5);
  color: var(--el-color-info);
}

.time {
  color: var(--el-text-color-secondary);
  font-size: 0.9rem;
}

.food-list :deep(.el-collapse) {
  border: none;
}

.food-list :deep(.el-collapse-item__header) {
  border: none;
  height: auto;
  padding: 0.5rem 0;
  background-color: var(--el-bg-color);
  transition: background-color 0.3s;
}

.food-list :deep(.el-collapse-item__header:hover) {
  background: linear-gradient(to right, var(--el-color-primary-light-9), transparent);
}

.food-list :deep(.el-collapse-item__content) {
  padding: 1rem;
  background: var(--el-fill-color-blank);
  border-radius: 4px;
  margin: 0 1rem 1rem;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

.food-item-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  min-height: 40px;
  flex: 1;
  padding: 0.5rem 1rem;
}

.food-basic-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.food-name {
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--el-text-color-primary);
}

.food-quantity {
  font-size: 0.9rem;
  color: var(--el-text-color-secondary);
}

.food-calories {
  font-size: 0.95rem;
  color: var(--el-color-primary);
  font-weight: 500;
  white-space: nowrap;
  padding: 2px 8px;
  background-color: var(--el-color-primary-light-9);
  border-radius: 4px;
}

.nutrition-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
  gap: 1rem;
  margin-bottom: 1rem;
}

.nutrition-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.8rem;
  background: linear-gradient(145deg, var(--el-bg-color), var(--el-fill-color-light));
  border-radius: 8px;
  transition: all 0.3s;
  border: 1px solid var(--el-border-color-lighter);
}

.nutrition-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  border-color: var(--el-color-primary-light-5);
}

.label {
  font-size: 0.9rem;
  color: var(--el-text-color-secondary);
}

.value {
  font-weight: 600;
  margin-top: 0.25rem;
  color: var(--el-color-primary);
  font-size: 1.1rem;
}

.food-actions {
  display: flex;
  justify-content: flex-end;
  gap: 1rem;
  padding-top: 0.5rem;
  border-top: 1px solid var(--el-border-color-lighter);
}

.food-actions .el-button {
  transition: all 0.3s;
}

.food-actions .el-button:hover {
  transform: translateY(-1px);
  opacity: 0.9;
}

.empty-state {
  padding: 2rem;
  background: linear-gradient(145deg, var(--el-bg-color), var(--el-fill-color-light));
  border-radius: 8px;
  text-align: center;
}

.empty-state :deep(.el-empty__image) {
  filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.1));
}

.empty-state :deep(.el-button) {
  margin-top: 1rem;
  transition: all 0.3s;
}

.empty-state :deep(.el-button:hover) {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

@media (max-width: 768px) {
  .card-header {
    padding: 8px 12px;
  }

  .food-item-header {
    flex-wrap: nowrap;
    gap: 0.5rem;
    padding: 0.5rem;
  }

  .food-basic-info {
    flex: 1;
    min-width: 0;
  }

  .food-name {
    font-size: 0.9rem;
  }

  .food-quantity {
    font-size: 0.8rem;
  }

  .food-calories {
    font-size: 0.8rem;
  }

  .nutrition-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 0.5rem;
  }

  .nutrition-item {
    padding: 0.25rem;
  }

  .food-list :deep(.el-collapse-item__content) {
    margin: 0 0.5rem 0.5rem;
    padding: 0.5rem;
  }
}

.food-item-header .el-avatar {
  flex-shrink: 0;
  border: 1px solid var(--el-border-color-lighter);
  background-color: var(--el-fill-color-light);
}
</style> 